﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace contactusform.dataaccess
{
    public class ContactRepository
    {
        private readonly ContactUsEntities _entityHandler;

        /// <summary>
        /// Public Constructor
        /// </summary>
        public ContactRepository()
        {
            _entityHandler = new ContactUsEntities();
        }

        /// <summary>
        /// Persist a Contact Entity into the database
        /// </summary>
        /// <param name="contactInfo">entity to persist into the database</param>
        /// <returns>True or false based on if it was persisted or not</returns>
        public bool Save(Contact contactInfo)
        {
            if (contactInfo.ContactID != Guid.Empty)
            {
                _entityHandler.Entry(contactInfo).State = EntityState.Modified;
            }
            else
            {
                contactInfo.ContactID = Guid.NewGuid();
                _entityHandler.Contacts.Add(contactInfo);
            }
          
            return (_entityHandler.SaveChanges() > 0);
        }

    }

}
